/**
 * created by dt 2025-05-26
 * 优先级系统演示
 */

using UnityEngine;
using DLib.Attribute;

namespace DLib.Attribute.Examples
{
    /// <summary>
    /// 优先级系统详细演示
    /// </summary>
    public class PriorityExample : MonoBehaviour
    {
        [Header("测试参数")]
        public int baseAttack = 100;
        public float equipmentBonus = 0.3f;  // 装备+30%
        public float skillBonus = 0.5f;      // 技能+50%
        public int specialBonus = 50;        // 特殊+50点

        private Attributes attributes;

        void Start()
        {
            attributes = new Attributes();
            attributes.OnAttributeChanged += OnAttributeChanged;
            
            RunPriorityTest();
        }

        void RunPriorityTest()
        {
            Debug.Log("=== 优先级系统演示 ===");
            
            // 设置基础值
            attributes.SetBaseValue(AttributeIds.Attack, baseAttack);
            Debug.Log($"基础攻击力: {baseAttack}");

            // 添加装备加成（优先级1）
            attributes.AddPercentageModifier(AttributeIds.Attack, equipmentBonus, ModifierPriority.Equipment);
            Debug.Log($"装备加成后: {attributes.GetCurrentValue(AttributeIds.Attack)}");

            // 添加技能加成（优先级2）
            attributes.AddPercentageModifier(AttributeIds.Attack, skillBonus, ModifierPriority.Buff);
            Debug.Log($"技能加成后: {attributes.GetCurrentValue(AttributeIds.Attack)}");

            // 添加特殊加成（优先级3）
            attributes.AddFlatModifier(AttributeIds.Attack, specialBonus, ModifierPriority.Special);
            Debug.Log($"特殊加成后: {attributes.GetCurrentValue(AttributeIds.Attack)}");

            Debug.Log("优先级确保计算顺序：装备→技能→特殊");
        }

        void OnAttributeChanged(int attributeId, int oldValue, int newValue)
        {
            if (attributeId == AttributeIds.Attack)
            {
                Debug.Log($"攻击力变化: {oldValue} → {newValue} (变化: {newValue - oldValue:+0;-0})");
            }
        }

        [ContextMenu("重新运行测试")]
        public void RerunTest()
        {
            attributes.Reset();
            RunPriorityTest();
        }

        [ContextMenu("演示不同优先级组合")]
        public void DemonstrateDifferentCombinations()
        {
            Debug.Log("=== 不同优先级组合演示 ===");
            
            // 组合1: 只有装备加成
            var attrs1 = new Attributes();
            attrs1.SetBaseValue(AttributeIds.Attack, baseAttack);
            attrs1.AddPercentageModifier(AttributeIds.Attack, equipmentBonus, ModifierPriority.Equipment);
            Debug.Log($"只有装备加成: {attrs1.GetCurrentValue(AttributeIds.Attack)}");

            // 组合2: 装备+技能
            var attrs2 = new Attributes();
            attrs2.SetBaseValue(AttributeIds.Attack, baseAttack);
            attrs2.AddPercentageModifier(AttributeIds.Attack, equipmentBonus, ModifierPriority.Equipment);
            attrs2.AddPercentageModifier(AttributeIds.Attack, skillBonus, ModifierPriority.Buff);
            Debug.Log($"装备+技能: {attrs2.GetCurrentValue(AttributeIds.Attack)}");

            // 组合3: 装备+技能+特殊
            var attrs3 = new Attributes();
            attrs3.SetBaseValue(AttributeIds.Attack, baseAttack);
            attrs3.AddPercentageModifier(AttributeIds.Attack, equipmentBonus, ModifierPriority.Equipment);
            attrs3.AddPercentageModifier(AttributeIds.Attack, skillBonus, ModifierPriority.Buff);
            attrs3.AddFlatModifier(AttributeIds.Attack, specialBonus, ModifierPriority.Special);
            Debug.Log($"装备+技能+特殊: {attrs3.GetCurrentValue(AttributeIds.Attack)}");

            // 组合4: 不同优先级顺序
            var attrs4 = new Attributes();
            attrs4.SetBaseValue(AttributeIds.Attack, baseAttack);
            attrs4.AddFlatModifier(AttributeIds.Attack, specialBonus, ModifierPriority.Base);
            attrs4.AddPercentageModifier(AttributeIds.Attack, equipmentBonus, ModifierPriority.Buff);
            attrs4.AddPercentageModifier(AttributeIds.Attack, skillBonus, ModifierPriority.Special);
            Debug.Log($"不同优先级顺序: {attrs4.GetCurrentValue(AttributeIds.Attack)}");
        }
    }
} 